package sg.edu.nus.iss.vmcs.controller;


import sg.edu.nus.iss.vmcs.util.VMCSException;

/**
 * 
 * @author Chomeaye
 *
 */

public class CoinReceiving implements CoinReceiverState {

	   private static CoinReceiving instance = null;
	   protected CoinReceiving() {
	      
	   }
	   public static CoinReceiving GetInstance() {
	      if(instance == null) {
	         instance = new CoinReceiving();
	      }
	      return instance;
	   }

	   
	@Override
	public void continueReceive(CoinReceiver coinReceiver) throws VMCSException {
		
			
		//set coinReceiving Sate
		coinReceiver.setState(this);
	}

	@Override
	public void receiveCoin(CoinReceiver coinReceiver, double totAmtInserted)
			throws VMCSException {
		
		try{
			
			coinReceiver.getTransactionController().displayTotalInsertedMoney(totAmtInserted);
		}catch(Exception e){
			e.printStackTrace();
		}

			
		}
		
		
	@Override
	public void refundCash(CoinReceiver coinReceiver) throws Exception {
			
			
		double totalInsertedMoney=coinReceiver.getTotalInserted();
		
		if (totalInsertedMoney >0) 
		{
			coinReceiver.getTransactionController().displayReutndMoney(totalInsertedMoney);
			
		}
			
		coinReceiver.setState(CoinReceiverIdle.GetInstance());
			
			
					
	}

	@Override
	public void startCoinReceiver(CoinReceiver coinReceiver)
			throws VMCSException {
	
		throw new VMCSException("CoinReceiving.StartCoinReceiver", "Coin Receiver is in coin receiving state.");
	}

	@Override
	public void startReceive(CoinReceiver coinReceiver) throws VMCSException {
				throw new VMCSException("CoinReceiving.StartReceive", "Coin Receiver is in coin receiving state.");
	}

	@Override
	public void stopCoinReceiver(CoinReceiver coinReceiver)
			throws VMCSException {
	
		throw new VMCSException("CoinReceiving.StartReceive", "Coin Receiver is in coin receiving state.");
		
	}

	@Override
	public void stopReceive(CoinReceiver coinReceiver) throws VMCSException {
	
			
		//refund money
		try {
			this.refundCash(coinReceiver);
		} catch (Exception e) {
			
			throw new VMCSException("CoinReceiving.StopReceive", "Error in stopReceive coin function.");
		}

	}

	@Override
	public boolean storeCash(CoinReceiver coinReceiver) throws VMCSException {
		
		
//		if (coinReceiver.getCoins() == null)
//			return false;
//		
//		for (int i = 0; i < coinReceiver.getCoins().length; i++){
//			try{
//				
//				//TODO : store coin 
//				
//			}
//			catch(Exception vmcse){
//				coinReceiver.setState(FaultDetected.GetInstance());
//				return false;
//			}
//		}
		
		//TODO : Store Cash
		
		//Set state CoinReceiverIdle			
		coinReceiver.setState(CoinReceiverIdle.GetInstance());
		return true;
		
		
	}

	@Override
	public void terminateFault(CoinReceiver coinReceiver) throws Exception {
	
		coinReceiver.setState(FaultDetected.GetInstance());
	}

}
